package cn.fotoit.product.system.controller;

import cn.fotoit.product.system.entity.RoleRelPermission;
import cn.fotoit.product.system.service.RoleRelPermissionService;
import cn.fotoit.product.util.ReturnBean;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "系统角色资源接口", tags = "系统角色资源相关接口，角色资源关联")
@RestController
@CrossOrigin
@RequestMapping("roleRelPermission")
public class RoleRelPermissionController {

    @Autowired
    private RoleRelPermissionService roleRelPermissionService;

    @PostMapping("add")
    @RequiresPermissions(value = {"system_role_add", "system_permission_add"})

    public ReturnBean add(
            @RequestParam Integer roleId,
            @RequestParam Integer PermissionId) {
        RoleRelPermission roleRelPermission = new RoleRelPermission();
        roleRelPermission.setRoleId(roleId);
        roleRelPermission.setPermissionId(PermissionId);
        roleRelPermissionService.add(roleRelPermission);
        return ReturnBean.success("添加成功");
    }

    @PostMapping("updateNotNull")
    @RequiresPermissions(value = {"system_role_update", "system_permission_update"})
    public ReturnBean updateNotNull(
            @RequestParam Integer id,
            @RequestParam Integer roleId,
            @RequestParam Integer PermissionId) {
        RoleRelPermission roleRelPermission = new RoleRelPermission();
        roleRelPermission.setId(id);
        roleRelPermission.setRoleId(roleId);
        roleRelPermission.setPermissionId(PermissionId);
        roleRelPermissionService.updateNotNull(roleRelPermission);
        return ReturnBean.success("修改成功");
    }

    @PostMapping("deleteById")
    public ReturnBean deleteById(@RequestParam Integer id) {
        roleRelPermissionService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findByRoleId")
    public ReturnBean findByRoleId(@RequestParam Integer roleId) {
        return ReturnBean.success("", roleRelPermissionService.findByRoleId(roleId));
    }

    @PostMapping("findByPermissionId")
    public ReturnBean findByPermissionId(@RequestParam Integer permissionId) {
        return ReturnBean.success("", roleRelPermissionService.findByPermissionId(permissionId));
    }

}
